package presentation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import control.CControl;

public class PSteeringWheel extends JPanel implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Double IMAGE_WIDTH=800.0;
	private Double IMAGE_HEIGHT=450.0;
	private double angle = 0;

	private Image carImage = null;
	
	public PSteeringWheel(CControl cControl)
	{
		this.setBackground(Color.white);
		cControl.addRegisterObserver("FSN_SteeringAngle",this);
		try {
			carImage = ImageIO.read(new File(cControl.getRootPath() + "img/wheel.jpg"));
		} catch (IOException e) {
			cControl.addError("Unable to access images for GUI: " + e.getMessage());
		}
		
		carImage = carImage.getScaledInstance(IMAGE_WIDTH.intValue()/2,IMAGE_HEIGHT.intValue()/2 , Image.SCALE_SMOOTH);
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		angle = (Double) (arg1) ;
		this.updateUI();
	}
	
	protected void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	Graphics2D g2 = (Graphics2D)g;
    	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    			RenderingHints.VALUE_ANTIALIAS_ON);
    	Rectangle rect = this.getBounds(); 
    	//creating the AffineTransform instance 
    	AffineTransform affineTransform = new AffineTransform(); 
    	//set the translation to the mid of the component 
    	int center_x,center_y;
    	center_x = (rect.width-IMAGE_WIDTH.intValue()/2);
    	center_y = (rect.height-IMAGE_HEIGHT.intValue()/2)/2;
    	affineTransform.setToTranslation(center_x,center_y); 
    	//rotate with the anchor point as the mid of the image 
    	affineTransform.rotate(Math.toRadians(angle), center_x,center_y); 
    	//draw the image using the AffineTransform 
    	g2.drawImage(carImage, affineTransform, this); 
    	
	}
	

}
